package com.wen.image;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.wen.image.bean.ImageBean;

import java.util.ArrayList;
import java.util.List;

import cn.wen.base.adapter.recyclerview.QuickRecyclerAdapter;
import cn.wen.base.adapter.recyclerview.QuickRecyclerViewHolder;


/**
 * 图片选择器的Adapter
 */

class SelectImageAdapter extends QuickRecyclerAdapter<ImageBean> {
    // 选择图片的集合
    private ArrayList<String> mResultImageList;
    private int mMaxCount;
    private OnSelectListener mOnSelectListener;

    public SelectImageAdapter(Context context, List<ImageBean> data, ArrayList<String> resultImageList, int maxCount) {
        super(context, data, R.layout.item_select_image);
        this.mResultImageList = resultImageList;
        this.mMaxCount = maxCount;
    }

    @Override
    protected void convert(QuickRecyclerViewHolder holder, final ImageBean item, final int position) {
        // 说明点击了拍照
        if (item.path == null) {
            ImageView squareImage = holder.getView(R.id.camera_image);
            squareImage.setScaleType(ImageView.ScaleType.CENTER);
            squareImage.setColorFilter(Color.parseColor("#00000000"));
            squareImage.setImageResource(R.drawable.takepic_normal);
            holder.setVisible(R.id.right_image, View.INVISIBLE);
            holder.setVisible(R.id.camera_image, View.VISIBLE);
            holder.setVisible(R.id.square_image, View.GONE);
            holder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 打开相机
                    if (mResultImageList.size() < mMaxCount) {
                        if (mOnSelectListener != null) {
                            mOnSelectListener.openCamera();
                        }
                    } else {
                        Toast.makeText(mContext, "最多选择" + mMaxCount + "张", Toast.LENGTH_SHORT).show();
                    }
                    return;
                }
            });
            return;
        } else {
            holder.setVisible(R.id.camera_image, View.GONE);
            holder.setVisible(R.id.square_image, View.VISIBLE);
            // 这是选择相片
            holder.setVisible(R.id.right_image, View.VISIBLE);
            holder.displayImage(R.id.square_image, item.path);
            ImageView squareImage = holder.getView(R.id.square_image);
            squareImage.setScaleType(ImageView.ScaleType.CENTER_CROP);
            ImageView rightImage = holder.getView(R.id.right_image);
            if (item.path != null && mResultImageList.contains(item.path)) {
                // 选中
                squareImage.setColorFilter(Color.parseColor("#66000000"));
                rightImage.setImageResource(R.drawable.ic_image_select);
            } else {
                // 正常
                squareImage.setColorFilter(Color.parseColor("#00000000"));
                rightImage.setImageResource(R.drawable.ic_image_normal);
            }
            holder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (item.path == null) {
                        return;
                    }
                    // 不包含
                    if (!mResultImageList.contains(item.path)) {
                        if (mResultImageList.size() < mMaxCount) {
                            mResultImageList.add(item.path);
                        }

                    } else {
                        // 包含
                        mResultImageList.remove(item.path);
                    }
                    if (mOnSelectListener != null) {
                        mOnSelectListener.onSelect(mResultImageList);
                    }
                    notifyItemChanged(position);
                }
            });
        }

    }

    public interface OnSelectListener {
        void onSelect(ArrayList<String> selectImages);

        void openCamera();
    }

    public void setOnSelectListener(OnSelectListener onSelectListener) {
        this.mOnSelectListener = onSelectListener;
    }
}
